package xyz.gpgl.erp.logic.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.base.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xyz.gpgl.erp.logic.entity.LogicShares;
import xyz.gpgl.erp.logic.entity.LogicSharesXpathDetail;
import xyz.gpgl.erp.logic.entity.LogicSharesXpathMaster;
import xyz.gpgl.erp.logic.entity.emeus.LogicSharesXpathDetailEmeus;
import xyz.gpgl.erp.logic.service.ILogicSharesService;
import xyz.gpgl.erp.logic.service.ILogicSharesXpathDetailService;
import xyz.gpgl.erp.logic.service.ILogicSharesXpathMasterService;
import xyz.gpgl.erp.logic.service.ISharesTaskService;

import java.util.*;

/**
 * @author liubo
 * @created 2019-10-26 22:03
 */
@Slf4j
@Component
public class SharesTask {

    private static final String start =  "0900";
    private static final String end =  "1700";

    @Autowired
    private ISharesTaskService iSharesTaskService;
    @Autowired
    private ILogicSharesService logicSharesService;

    @Autowired
    private ILogicSharesXpathMasterService logicSharesXpathMasterService;
    @Autowired
    private ILogicSharesXpathDetailService logicSharesXpathDetailService;

    /**
     * 监视大盘获取数据并分心
     * 每天3点执行 一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void monitorStockChanges(){
        if(isContinue(start,end)){
            return;
        }
        QueryWrapper<LogicSharesXpathMaster> xpathQuery = new QueryWrapper<>();
        xpathQuery.last(" limit 1");
        LogicSharesXpathMaster  xpath= this.logicSharesXpathMasterService.getOne(xpathQuery);
        if (CommonUtil.isEmpty(xpath)){
            log.error("没有配置xpath");
            return;
        }
        Map<String, LogicSharesXpathDetail> xpathMap = new HashMap<>();
        QueryWrapper<LogicSharesXpathDetail> detailsWrapper = new QueryWrapper();
        detailsWrapper.eq(LogicSharesXpathDetailEmeus.masterId, xpath.getId());
        List<LogicSharesXpathDetail> details = this.logicSharesXpathDetailService.list(detailsWrapper);
        if (CommonUtil.isEmpty(details)){
            log.error("xpath没后具体配置项");
            return;
        }
        xpathMap .putAll(CommonUtil.getCollectionFieldToMap(details,
                CommonUtil.dbForDBToFistLowercaseHump(LogicSharesXpathDetailEmeus.sharesKey)));
        List<LogicShares> shares = logicSharesService.list();
        if (CommonUtil.isEmpty(shares)){
            log.error("没有具体的股票信息");
        }
        // 股票扫描
        for (LogicShares share : shares) {
            iSharesTaskService.monitorStockChanges(share ,xpath,xpathMap);
        }
        //消息发送
        logicSharesService.stockPriceScanning(shares);
    }



    private boolean isContinue(String start, String end) {
        String hh = CommonUtil.getDateFormat(new Date(), "HHmm");
        if(hh.compareTo(start)<0||hh.compareTo(end)>0){
            log.info("监视股票变化->闭市时间");
            return true;
        }

        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) ;
        if(dayOfWeek ==1 || dayOfWeek == 7 ){
            log.info("监视股票变化->闭市时间-只有周一到周五开市[{}]",dayOfWeek);
            return true;
        }
        return false;
    }

}
